$(function() {
	
});

function doSearch() {
	var tableName = $('#tableName').val();
	var start = $('#start').datebox('getValue');
	var end = $('#end').datebox('getValue');
    var page = $(".pagination-num").val();
    var rows = $(".pagination-page-list option:selected").text();
	$.ajax({
		url:'/extractingData/getData',
		type:'post',
		dataType:'json',
		data:{tableName : tableName, page : page, rows : rows, startTime : start, endTime : end},
		success: function(data) {
			if (data.success) {
				var html = '';
				html = html +'<div class="ibox-content"><table class="table table-bordered"><thead><tr><th>#</th>';
				for (var item in data.data[0]) {
					html = html + '<th>' + item + '</th>';
				}
				html = html + '</tr></thead><tbody>';
				var number = 0;
				for (var i = 0; i < data.data.length; i++) {
					number ++;
					html = html + '<tr>';
					html = html + '<td>' + number +'</td>';
					for (var item in data.data[i]) {
						html = html + '<td>' + data.data[i][item] +'</td>';
						}
					html = html + '</tr>';
				}
				html = html + '</tbody></table></div>';
			    html = html + '</style></div><div class="datagrid-pager pagination"><table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td><select class="pagination-page-list">'
			    +'<option value="100">100</option><option value="200">200</option><option value="400">400</option><option value="500">500</option><option value="1000">1000</option></select></td><td><div class="pagination-btn-separator"></div></td><td><a href="javascript:void(0)" class="l-btn l-btn-plain l-btn-disabled l-btn-plain-disabled" group="" id="">'
                +'<span class="l-btn-left"><span class="l-btn-text"><span class="l-btn-empty pagination-first">&nbsp;</span></span></span>'
                +'</a></td><td><a href="javascript:void(0)" class="l-btn l-btn-plain" group="" id=""><span class="l-btn-left"><span class="l-btn-text"><span class="l-btn-empty pagination-prev" onClick="prePage()">&nbsp;</span></span></span></a></td><td><div class="pagination-btn-separator"></div></td><td><span style="padding-left:6px;">第</span>'
                +'</td><td><input class="pagination-num" type="text" value=' + data.pageValue + ' size="2"></td><td><span style="padding-right:6px;">共'
                + data.totalPage + '页</span></td><td><div class="pagination-btn-separator"></div></td><td><a href="javascript:void(0)" class="l-btn l-btn-plain" group="" id=""><span class="l-btn-left"><span class="l-btn-text"><span class="l-btn-empty pagination-next" onClick = "nextPage()">&nbsp;</span></span></span></a></td><td><a href="javascript:void(0)" class="l-btn l-btn-plain" group="" id="">'
                +'<span class="l-btn-left"><span class="l-btn-text"><span class="l-btn-empty pagination-last" onClick="lastPage()">&nbsp;</span></span></span></a></td><td><div class="pagination-btn-separator"></div></td><td><a href="javascript:void(0)" class="l-btn l-btn-plain" group="" id=""><span class="l-btn-left"><span class="l-btn-text"><span class="l-btn-empty pagination-load" onClick="reloadPage()">&nbsp;</span></span></span></a></td></tr></tbody></table>'
                +'<div class="pagination-info">共'+data.total+'条记录</div><div style="clear:both;"></div></div></div></div><input type="hidden" id= "allTotal" value='+data.totalPage+'>';
			    $('#mainPanle').html(html);
			    $(".pagination-page-list").find("option[value ='"+data.rowsValue+"']").attr("selected",true);
			} else {
				$.messager.alert("提示",data.msg)
			}
		}
	})
}

function nextPage() {
	var now = $('.pagination-num').val();
	$('.pagination-num').val(parseInt(now) + 1); 
	doSearch();
}

function lastPage() {
	var total = $('#allTotal').val();
	$('.pagination-num').val(total); 
	doSearch();
}

function reloadPage() {
	doSearch();
}

function prePage() {
	var now = $('.pagination-num').val();
	if (now - 1 < 1) {
		$.messager.alert("提示", "页码不能小于1");
		return;
	}
	$('.pagination-num').val(parseInt(now) - 1); 
	doSearch();
}


